//
//  GJFClassifyViewController.m
//  Wedding
//
//  Created by 郭静芬 on 2016/12/24.
//  Copyright © 2016年 郭静芬. All rights reserved.
//

#import "GJFClassifyViewController.h"
#import "GJFPlotTableViewCell.h"
#import "GJFPlotModel.h"
#import "GJFPlotDetailViewController.h"

@interface GJFClassifyViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page_index;
@end

@implementation GJFClassifyViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page_index = 1;
    [self getData];
}

#pragma mark - 获取数据
- (void)getData {
    NSDictionary *dict = @{@"item":self.classifyTitle?:@"", @"page_index":@1};
    [GJFNetworkTool POSTRequestWithUrl:APP_PLOT params:dict complete:^(NSDictionary *result) {
        [self.tableView.mj_header endRefreshing];
        if ([[result objectForKey:@"code"] intValue] == 1000) {
            self.dataArr = [GJFPlotModel transformWithArr:[result objectForKey:@"data"]];
            [self.tableView reloadData];
        }
        else {
            [self.view showWarning:[result objectForKey:@"msg"]];
        }
    }];
}
#pragma mark - 获取更多数据
- (void)getMoreData {
    NSDictionary *dict = @{@"item":self.classifyTitle?:@"", @"page_index":@(++_page_index)};
    [GJFNetworkTool POSTRequestWithUrl:APP_PLOT params:dict complete:^(NSDictionary *result) {
        [self.tableView.mj_footer endRefreshing];
        if ([[result objectForKey:@"code"] intValue] == 1000) {
            [self.dataArr addObjectsFromArray:[GJFPlotModel transformWithArr:[result objectForKey:@"data"]]];
            [self.tableView reloadData];
        }
        else {
            self.page_index--;
            [self.view showWarning:[result objectForKey:@"msg"]];
        }
    }];
}


#pragma mark - tableView - delegate & dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GJFPlotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CarouseCell"];
    cell.plotModel = self.dataArr[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 275 * YY;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GJFPlotDetailViewController *plotDetailVC = [[GJFPlotDetailViewController alloc] init];
    plotDetailVC.plotId = [self.dataArr[indexPath.row] id];
    plotDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:plotDetailVC animated:YES];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[GJFPlotTableViewCell class] forCellReuseIdentifier:@"CarouseCell"];
        [self.view addSubview:_tableView];
        MJRefreshGifHeader *header = [GJFRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        _tableView.mj_header = header;
        
        _tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
